package com.yoki.cloud.common.code.bean;

import com.yoki.cloud.common.code.db.property.DBColumnProperty;
import com.yoki.cloud.common.code.db.property.DBTableProperty;
import com.yoki.cloud.common.code.mapper.MySqlMapper;

import java.util.ArrayList;
import java.util.List;

/**
 * 将数据库表、数据库表列java bean类，转换为对应的java实体类
 * Created by SKY on 2017/7/23.
 */
public class Transformer {

    public List<EntityBean> changeTableList(List<DBTableProperty> tableList) {
        List<EntityBean> list = new ArrayList<>();
        for (DBTableProperty table : tableList) {
            list.add(change(table));
        }
        return list;
    }

    /**
     * 将数据库表bean转换为实体类EntityBean
     *
     * @param table
     * @return
     */
    public EntityBean change(DBTableProperty table) {
        EntityBean bean = new EntityBean();
        String tableName = table.getTableName();
        // 将表名称根据"_"进行切割
        String[] splits = tableName.split("_");
        if (null != splits && splits.length > 0) {
            StringBuffer className = new StringBuffer();
            for (int i = 0; i < splits.length; i++) {
                String s = splits[i];
                if (i == 0) {
                    bean.setModuleName(s.toLowerCase());
                }
                if (i == 0 && !"sys".equals(s) && !"res".equals(s)) {
                    continue;
                }
                // 将切割后的字符串拼接成class类名称，使用驼峰命名法
                className.append(s.substring(0, 1).toUpperCase()
                        + s.substring(1, s.length()));
            }
            // 注入表名称
            bean.setTableName(tableName);
            // 注入类名称
            bean.setClassName(className.toString());
            // 生成class对象名称，使用驼峰命名法，并注入
            bean.setClassObjectName(className.substring(0, 1).toLowerCase() +
                    className.substring(1, className.length()));
            bean.setDbTableProperty(table);


            /*bean.setProjName(cfg.getProjName());
            bean.setProjVersion(cfg.getProjVersion());
            bean.setMavenName(cfg.getMavenName());
            bean.setGroupId(cfg.getGroupId() + "." + cfg.getModuleName());*/
        }
        bean.setRemarks(table.getRemarks());

        bean.setAuthority("public");
        List<DBColumnProperty> columnList = table.getDBColumnPropertyList();
        List<FieldBean> fieldList = new ArrayList<>(10);
        // 迭代数据库表列属性列表
        for (DBColumnProperty column : columnList) {
            // 将数据库表列转换为FieldBean
            FieldBean field = change(column);
            if (null != field.getFieldClazz()) {
                // 生成需要导入的java类包路径，并注入进entityBean
                bean.addImport(field.getFieldClazz().getName());
            }
            fieldList.add(field);
        }
        // 将FieldBean列表注入进entityBean
        bean.setFields(fieldList);
        if (fieldList.size() > 0) {
            bean.setKeyName(fieldList.get(0).getFieldName());
            bean.setPrimaryKeyType(fieldList.get(0).getFieldType());
            bean.setKeyNameUp(bean.getKeyName().substring(0, 1).toUpperCase() + bean.getKeyName().substring(1));
            bean.setKeyDbName(fieldList.get(0).getDbColumnProperty().getColumnName());
        }

        return bean;
    }

    public List<FieldBean> changeColumnList(List<DBColumnProperty> columnList) {
        List<FieldBean> list = new ArrayList<>();
        for (DBColumnProperty column : columnList)
            list.add(change(column));
        return list;
    }

    /**
     * 将数据库表列转换为字段bean
     *
     * @param column
     * @return
     */
    public FieldBean change(DBColumnProperty column) {
        FieldBean field = new FieldBean();
        String columnName = column.getColumnName();
        // 将列名称根据"_"进行切割
        String[] splits = columnName.split("_");
        if (null != splits && splits.length > 0) {
            StringBuffer fieldNameUp = new StringBuffer();
            for (int i = 0; i < splits.length; i++) {
                String s = splits[i];
                if (i == 0 && splits.length > 1) {
                    // 判断是否有"fd"、"col"前缀，有的话忽略，不计入字段名称
                    if ("fd".equals(s) || "col".equals(s)) {
                        continue;
                    }
                }
                // 将切割后的字符串拼接成class类名称，使用驼峰命名法
                fieldNameUp.append(s.substring(0, 1).toUpperCase() + s.substring(1, s.length()));
            }
            field.setFieldNameUP(fieldNameUp.toString());
            field.setFieldName(fieldNameUp.substring(0, 1).toLowerCase() + fieldNameUp.substring(1, fieldNameUp.length()));
            field.setAuthority("private");
            field.setFieldClazz(MySqlMapper.getJavaType(column.getDataType()));
            field.setFieldType(MySqlMapper.getJavaType(column.getDataType()).getSimpleName());

            field.setFieldDbName(column.getColumnName());
            field.setLength(column.getColumnSize());
            field.setRemarks(column.getRemarks());
            field.setNullable(null != column.getIsNullable() &&
                    "yes".equals(column.getIsNullable().toLowerCase()));

        }
        field.setDbColumnProperty(column);
        return field;
    }

}
